"""
@name : ip作业
@author : zhaosiyuan
@projectname: PythonProject
"""
import subprocess
from IPy import IP

ip = input("请输入您要检测的ip：")
try:
    # 网址按.切开
    line = ip.split('.')
    if len(line) != 4:
        print("ip地址长度不正确，即将退出")
        raise SyntaxError
    # elif len(line) == 4:
    #     for item in line:
    #         # 对网址各段进行判断是否为数字
    #         if item.isdigit():
    #             if
    else:
        ip1 = IP(ip)
        for x in ip1:
            # print(x)
            result = subprocess.Popen('ping %s'%x, stdout=subprocess.PIPE)
            # print(result)
            result = result.stdout.read().decode('gbk')
            # print(result)
            if "请求超时" in result:
                with open("不存活ip.txt", "a+") as fp:
                    fp.write(str(x)+'\n')
            elif "TTL" in result:
                with open("存活ip.txt", "a+") as fp:
                    fp.write(str(x)+'\n')
except SyntaxError as e:
    print("长度不正确")